using System;
using System.Windows.Forms;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.Faces.Base;
using Habanero.Faces.Win;

namespace FireStarterModeller.UI
{
    /// <summary>
    /// View all the Domain Objects in a Solution.
    /// </summary>
    public class ViewAllDomainObjectsInSolution : UserControlWin
    {
        private readonly ITreeView _treeView;
        private readonly TreeViewController _treeViewController;
        private DMSolution _dmSolution;
        private readonly ImageList _imageList;
        /// <summary>
        /// Create a <see cref="ViewAllDomainObjectsInSolution"/>
        /// </summary>
        /// <param name="controlFactory"></param>
        public ViewAllDomainObjectsInSolution(IControlFactory controlFactory)
        {
            ControlFactory = controlFactory;
            _treeView = controlFactory.CreateTreeView();

            _imageList = new ImageList();
            _imageList.Images.Add("None", FMResources.Folder);
            _imageList.Images.Add("Module", FMResources.Module);
            _imageList.Images.Add("Entity", FMResources.entityimage2);
            ((TreeViewWin)_treeView).ImageList = _imageList;
            _treeViewController = new TreeViewController(_treeView);
            BorderLayoutManager layoutManager = controlFactory.CreateBorderLayoutManager(this);
            layoutManager.AddControl(_treeView, BorderLayoutManager.Position.Centre);

            _treeViewController.SetupNodeWithBusinessObject = ((node, businessObject) => SetNodeImage(businessObject, node));

            _treeViewController.SetupNodeWithRelationship = SetupNodeWithRelationship;
        }

        private void SetupNodeWithRelationship(ITreeNode node, IRelationship relationship)
        {
            const string imageName = "None";
            node.SelectedImageIndex = GetImageIndex(imageName);
            node.ImageIndex = GetImageIndex(imageName);
        }

        private void SetNodeImage(IBusinessObject businessObject, ITreeNode node)
        {
            string imageName;
            Type type = businessObject.GetType();
            if (type == typeof(DMAssembly))
            {
                imageName = "Module";
            }
            else if (type == typeof(DMClass) )
            {
                imageName = "Entity";
            }
            else
            {
                imageName = "None";
            }           
            node.SelectedImageIndex = GetImageIndex(imageName);
            node.ImageIndex = GetImageIndex(imageName);
        }


        private int GetImageIndex(string imageName)
        {
            return _imageList.Images.IndexOfKey(imageName);
        }

        public IControlFactory ControlFactory { get; private set; }

        public DMSolution DMSolution
        {
            get { return _dmSolution; }
            set
            {
                try
                {
                    _dmSolution = value;
                    _treeViewController.LoadTreeView(_dmSolution, 2);
                }
                catch (Exception ex)
                {
                    GlobalRegistry.UIExceptionNotifier.Notify(ex, "", "Error ");
                }
            }
        }

        #region IBOParentChildrenSelector Members

        public IBusinessObject BusinessObject
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        #endregion




        public void CleanUp()
        {
            _treeViewController.CleanUp();
        }
    }
}